package com.ruowei.modules.sys.utils;

import com.ruowei.config.ApplicationProperties;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class ClientSocket {

//    public static final Logger log = LoggerFactory.getLogger(ClientSocket.class);
//    private Socket socket;
//    private BufferedWriter writer;
//    private BufferedReader reader;
//
//    public void startConnection(ApplicationProperties.Esb esb) {
//        try {
//            socket = new Socket(esb.getIp(), esb.getPort());
//            writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8));
//            reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
//        } catch (IOException e) {
//            log.info("创建Socket异常：{}, {}", e.getCause(), e.getMessage());
//        }
//    }
//
//    public String sendMessage(String msg) {
//        String reqMsg = StringUtils.leftPad(String.valueOf(msg.getBytes(StandardCharsets.UTF_8).length), 8, '0') + msg;
//        log.info("请求报文：{}", reqMsg);
//        StringBuilder resp = new StringBuilder();
//        try {
//            writer.write(reqMsg);
//            writer.newLine();
//            writer.flush();
//            String line;
//            while ((line = reader.readLine()) != null) {
//                resp.append(line);
//            }
//        } catch (IOException e) {
//            log.info("发送消息异常：{}, {}", e.getCause(), e.getMessage());
//        }
//        return resp.toString();
//    }
//
//    public void stopConnection() {
//        try {
//            reader.close();
//            writer.close();
//            socket.close();
//        } catch (IOException e) {
//            log.info("关闭Socket异常：{}, {}", e.getCause(), e.getMessage());
//        }
//    }
}
